<?php

namespace Managers;

class PdfImage extends BaseManager
{
    const TABLE = '\Catalog\PdfImage';
    protected $__table = self::TABLE;

    public function add($pdfId, array $images)
    {
        $pdfRef = $this->_em->getReference(PdfFile::TABLE, $pdfId);
        if (!$pdfRef) {
            return false;
        }
        foreach ($images as $item) {
            $entity = new \Catalog\PdfImage();
            $entity->setUrl($item);
            $entity->setFile($pdfRef);
            $this->_em->persist($entity);
        }
        $this->_em->flush();
    }

    public function getByFile($fileId)
    {
        $qb = $this->createQueryBuilder()
            ->andWhere('pd.file = :file')->setParameter('file', $fileId);
        $res = $qb->getQuery()->getArrayResult();
        return $res;
    }

    protected function createQueryBuilder()
    {
        $qb = $this->_em->createQueryBuilder()
            ->select('pd')
            ->from(self::TABLE, 'pd');
        return $qb;
    }
}
